<?php namespace Summer\Hjd;

use System\Classes\PluginBase;

use Backend;
use BackendMenu;
use ReflectionClass;
use SystemException;

class Plugin extends PluginBase
{
    public function registerComponents()
    {
    }

    public function registerSettings()
    {
        return [
            'settings' => [
                'label'       => 'HJD管理',
                'description' => 'HJD分销系统配置',
                'category'    => 'HJD',
                'icon'        => 'icon-life-ring',
                'class'       => 'Summer\Hjd\Models\Settings',
                'permissions' => ['summer.hjd.permissions.sms_base'],
                'order'       => 600,
            ]
        ];
    }

    public function registerNavigation()
    {
    }
    public function register()
    {
        //注册控制台命令
        $this->registerConsoleCommand('hjd.bonus_fenhong', 'Summer\Hjd\Console\BonusFenhong');

        $configuration = $this->getConfigurationFromYaml();
        if (array_key_exists('navigation', $configuration)) {
            $navigation = $configuration['navigation'];
            if (is_array($navigation)) {
                array_walk_recursive($navigation, function (&$item, $key) {
                    if ($key === 'url') {
                        $item = Backend::url($item);
                    }
                });
            }
            foreach ($navigation as $nak=>$nav){
                BackendMenu::registerCallback(function ($manager) use($nak,$nav){
                    $manager->registerMenuItems('Winter.System', [$nak=>$nav]);
                });
                BackendMenu::registerContextSidenavPartial(
                    'Winter.System',
                    $nak,
                    '~/plugins/summer/hub/partials/_system_sidebar.htm'
                );
            }
        }
    }
    public function registerSideNavSettings()
    {
        $sideNav=[];
        $configuration = $this->getConfigurationFromYaml();
        if (array_key_exists('navigation', $configuration)) {
            $navigation = $configuration['navigation'];
            if (is_array($navigation)) {
                array_walk_recursive($navigation, function (&$item, $key) {
                    if ($key === 'url') {
                        $item = Backend::url($item);
                    }
                });
            }
            foreach ($navigation as $nak=>$nav){
                $sideMenu=$nav['sideMenu']??[];
                foreach ($sideMenu as $sidek=>&$sidrv){
                    $sidrv['context']=$sidrv['context']??$nak;
                    $sidrv['description']=$sidrv['description']??'';
                }
                $sideNav = array_merge($sideNav, $sideMenu);
            }
        }
        return $sideNav;
    }

}
